﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using System.Reflection;

namespace FormMdiTest
{
    public static class LogHelper
    {
        private static Logger _apiLogger = null;

        private static Logger ApiLogger
        {
            get
            {
                return _apiLogger = _apiLogger ?? LogManager.GetLogger("webapi");
            }
        }

        /// <summary>
        /// 判断是否打开info日志开关，在某些耗时操作时先进行判断
        /// </summary>
        /// <returns>是否打开info日志</returns>
        public static bool IsLoggingInfo()
        {
            return ApiLogger != null && ApiLogger.IsInfoEnabled;
        }

        /// <summary>
        /// 判断debug 日志是否打开
        /// </summary>
        /// <returns></returns>
        public static bool IsDebugEnabled()
        {
            return ApiLogger != null && ApiLogger.IsDebugEnabled;
        }

        /// <summary>
        /// 设置线程会话活动Id
        /// </summary>
        public static void SetActivityId(string activityId = null)
        {
            Trace.CorrelationManager.ActivityId = string.IsNullOrEmpty(activityId) ? Guid.NewGuid() : new Guid(activityId);
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            message += Assembly.GetExecutingAssembly().GetName().Name + Assembly.GetExecutingAssembly().GetName().Version;
            ApiLogger.Info(message);
        }

        ///// <summary>
        ///// 业务错误日志
        ///// </summary>
        ///// <param name="errorCode">错误码</param>
        ///// <param name="message">信息补充描述</param>
        //public static void Error(ErrorCode errorCode, string message)
        //{
        //    LogEventInfo theEvent = new LogEventInfo();
        //    theEvent.Properties["ErrorCode"] = ((int)errorCode).ToString();
        //    theEvent.Properties["ErrorName"] = errorCode.ToString();
        //    theEvent.Properties["ErrorDesc"] = errorCode.Desc();
        //    ApiLogger.Error(message, theEvent);
        //}

        ///// <summary>
        ///// 异常日志
        ///// </summary>
        ///// <param name="errorCode">错误码</param>
        ///// <param name="message">异常补充描述</param>
        ///// <param name="ex">异常信息</param>
        //public static void Fatal(ErrorCode errorCode, string message, Exception ex)
        //{
        //    LogEventInfo theEvent = new LogEventInfo();
        //    theEvent.Properties["ErrorCode"] = ((int)errorCode).ToString();
        //    theEvent.Properties["ErrorName"] = errorCode.ToString();
        //    theEvent.Properties["ErrorDesc"] = errorCode.Desc();
        //    ApiLogger.Fatal(ex, message, theEvent);
        //}
    }

}
